﻿// -----------------------------------------------------------------------
// <copyright file="BufferStream.cs">
// Copyright (c) 2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace PortableImagingLibrary.Png.Compressors
{
    using System;
    using System.Diagnostics.Contracts;
    using System.IO;

    internal sealed class BufferStream : Stream
    {
        private byte[] _buffer;

        /// <summary>Gets a value indicating whether the current stream supports reading. </summary>
        /// <returns>true if the stream supports reading; otherwise, false.</returns>
        public override bool CanRead { get { return true; } }

        /// <summary>Gets a value indicating whether the current stream supports seeking. </summary>
        /// <returns>true if the stream supports seeking; otherwise, false.</returns>
        public override bool CanSeek { get { return true; } }

        /// <summary>Gets a value indicating whether the current stream supports writing. </summary>
        /// <returns>true if the stream supports writing; otherwise, false.</returns>
        public override bool CanWrite { get { return true; } }

        /// <summary>Gets the length in bytes of the stream. </summary>
        /// <returns>A long value representing the length of the stream in bytes.</returns>
        public override long Length { get { return _buffer.Length; } }

        /// <summary>Gets or sets the position within the current stream. </summary>
        /// <returns>The current position within the stream.</returns>
        public override long Position { get; set; }

        /// <summary>Initializes a new instance of the <see cref="BufferStream"/> class.</summary>
        public BufferStream()
        {
            this._buffer = null;
        }

        /// <summary>Initializes a new instance of the <see cref="BufferStream"/> class. </summary>
        /// <param name="buffer">The initial buffer.</param>
        public BufferStream(byte[] buffer)
        {
            Contract.Requires(buffer != null);

            this._buffer = buffer;
            this.Position = 0;
        }

        /// <summary>Sets new data for the stream.</summary>
        /// <param name="buffer">The data.</param>
        public void UpdateBuffer(byte[] buffer)
        {
            Contract.Requires(buffer != null);

            this._buffer = buffer;
            this.Position = 0;
        }

        /// <summary>Clears all buffers for this stream and causes any buffered data to be written to the underlying device. </summary>
        public override void Flush()
        {
        }

        /// <summary>Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. </summary>
        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset" /> and (<paramref name="offset" /> + <paramref name="count" /> - 1) replaced by the bytes read from the current source.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> at which to begin storing the data read from the current stream.</param>
        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
        /// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. </returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            int realCount = (this.Position + count) > this._buffer.Length ?
                (int)(this._buffer.Length - this.Position) :
                count;

            Buffer.BlockCopy(this._buffer, (int)this.Position, buffer, offset, realCount);

            this.Position += realCount;

            return realCount;
        }

        /// <summary>Sets the position within the current stream. </summary>
        /// <param name="offset">A byte offset relative to the <paramref name="origin" /> parameter.</param>
        /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to obtain the new position.</param>
        /// <returns>The new position within the current stream. </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">Invalid <paramref name="origin" /> value.</exception>
        public override long Seek(long offset, SeekOrigin origin)
        {
            switch (origin) {
                case SeekOrigin.Begin:
                    this.Position = offset;
                    break;

                case SeekOrigin.Current:
                    this.Position += offset;
                    break;

                case SeekOrigin.End:
                    this.Position = this._buffer.Length - offset;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("origin");
            }

            return this.Position;
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }
    }
}
